<?php

namespace app\admin\controller;

use app\common\controller\publics\Jwt;
use think\Controller;
use think\Db;
use think\Env;
use think\facade\Cookie;
use think\Request;
use Zewail\Api\JWT\Factories\Code;

class Login extends Controller
{
    public function initialize(){
        error_reporting(5);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        //

        //例: xxx.xxx.xxx/admin/Login/index/id/value1/a_id/value2
        $id = $request->param('id'); // value1
        $a_id = $request->param('a_id'); //value2
        ($id) or ($id=1);
        //检查是否是管理员
        $member = Db::name('member')->where('member_id','=',$id)->where('is_admin','in',[1,2])->find();
        $admin_list = Db::name('member')->field('member_id,member_user')->where('is_admin','in',[1,2])->select();

        $this->assign('member',$member);
        $this->assign('admin_list',$admin_list);
        $this->assign('title','登录');

        return $this->fetch();
    }

    public function login(Request $request){
        //token 验证登录信息是否有效
        $request = new Request();
        $g=$request->get();
        $p = $request->post();
        $jwt = new \app\common\controller\publics\Jwt();
        if($jwt->is_token($p['token'])){
            $this->success('你已经登录，正在跳转到首页','/?token='.$p['token']);
        }



        if($id = $p['member_id']){
            $member = Db::name('member')->where('member_id','=',$id)->where('member_user','=',$p['member_user'])->find();
            if(md5_pass($p['member_password'],$member['member_salt'])===$member['member_password']){
                $cookie_time = isset($p['cookie_time'])?$p['cookie_time']:0;
                Cookie::prefix('fwf_');
                if($cookie_time == 1){
                    Cookie::forever('admin_id',$member['member_id']);
                    Cookie::forever('admin_name',$member['member_user']);
                    Cookie::forever('is_admin',$member['is_admin']);
                }else{
                    $time = 3600*24*7;
                    Cookie::set('admin_id',$member['member_id'],$time);
                    Cookie::set('admin_name',$member['member_user'],$time);
                    Cookie::set('is_admin',$member['is_admin'],$time);
                }
                $jwt = new Jwt();
                if($p['checked']){
                    $token = $jwt->create_token(3600*24*7,[
                        'id'            => $member['member_id'],
                        'name'          => $member['member_user'],
                        'is_admin'      => $member['is_admin'],
                        'ip'            => $_SERVER['REMOTE_ADDR'],
                        'user_agent'    => md5($_SERVER['HTTP_USER_AGENT']),
                    ]);
                }else{
                    $token = $jwt->create_token(1200,[
                        'id'            => $member['member_id'],
                        'name'          => $member['member_user'],
                        'is_admin'      => $member['is_admin'],
                        'ip'            => $_SERVER['REMOTE_ADDR'],
                        'user_agent'    => md5($_SERVER['HTTP_USER_AGENT']),
                    ]);
                }
                fwf_log('管理员登录，管理员名称：'.$member['member_user'].'管理员ID：'.$member['member_id']);
                set_LO('token',$token);
                $this->success("登录成功",'/?token='.$token);

            }else{
                $this->error(lang('pass_error'));
            }
        }

    }
    public function login_out(Request $request){
        cookie(null,'fwf_');
        set_LO('token','');
        //$this->redirect('/login');
        js_code('location.href="/admin_login";');
    }


}
